/*
 * Powered By dico
 * Since 2017 - 2018
 */
package com.seeyoui.kensite.bussiness.orderlang.productPrice.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seeyoui.kensite.bussiness.orderlang.productPrice.domain.ProductPrice;
import com.seeyoui.kensite.bussiness.orderlang.productPrice.domain.ProductPriceSpecDTO;
import com.seeyoui.kensite.bussiness.orderlang.productPrice.service.ProductPriceService;
import com.seeyoui.kensite.bussiness.orderlang.productSpec.domain.ProductSpec;
import com.seeyoui.kensite.bussiness.orderlang.productSpec.service.ProductSpecService;
import com.seeyoui.kensite.bussiness.orderlang.warehouseProduct.service.WarehouseProductService;
import com.seeyoui.kensite.common.base.controller.BaseController;
import com.seeyoui.kensite.common.base.domain.EasyUIDataGrid;
import com.seeyoui.kensite.common.base.domain.Page;
import com.seeyoui.kensite.common.constants.StringConstant;
import com.seeyoui.kensite.common.util.DateUtils;
import com.seeyoui.kensite.common.util.RequestResponseUtil;
import com.seeyoui.kensite.common.util.Result;
import com.seeyoui.kensite.common.util.excel.ExportExcel;
import com.sun.star.io.TempFile;
/**
 * ProductPrice
 * @author dico
 * @version 2.0
 * @since 1.0
 * @date 2018-02-07
 */

@Controller
@RequestMapping(value = "productPrice/productPrice")
public class ProductPriceController extends BaseController {
	
	@Autowired
	private ProductPriceService productPriceService;
	@Autowired
	private ProductSpecService productSpecService;
	@Autowired
	private WarehouseProductService warehouseProductService;
	
	@RequestMapping(value = "/findOneByProductId", method=RequestMethod.POST)
	@ResponseBody
	public Result findOneByProductId(String productId){
		ProductPrice productPrice = productPriceService.findOneByProductId(productId);
		if(productPrice != null){
			return Result.success(productPrice);
		}else{
			return Result.failure("查询失败");
		}
	}
	
	@RequestMapping(value = "/findPriceSpecByProductId", method=RequestMethod.POST)
	@ResponseBody
	public Result findPriceSpecByProductId(String productId){
		List<ProductPriceSpecDTO> priceList = productPriceService.findPriceByProductId(productId);
		for (ProductPriceSpecDTO productPriceSpecDTO : priceList) {
			List<ProductSpec> specList = productSpecService.findListByProductPriceId(productPriceSpecDTO.getId());
			productPriceSpecDTO.setProductSpecList(specList);
		}
		if(priceList.size() > 0){
			return Result.success(priceList);
		}else{
			return Result.failure("查询失败");
		}
	}
	
	/**
	 * 根据规格值查询价格，库存
	 * @param productId
	 * @param specValue
	 * @return
	 */
	@RequestMapping(value = "/findPriceBySpecValue", method=RequestMethod.POST)
	@ResponseBody
	public Result findPriceBySpecValue(String productId,String specValue){
		List<ProductSpec> productSpecList = productSpecService.findListByProductId(productId);
		List<String> specValueList = Arrays.asList(specValue.split(","));
		List<String> priceIdList = new ArrayList<String>();
		for (ProductSpec productSpec : productSpecList) {
			for (String specValueString : specValueList) {
				if(specValueString.equals(productSpec.getSpecValue())){
					priceIdList.add(productSpec.getProductPriceId());
				}
			}
		}
		String regex;  
	    Pattern p;  
	    Matcher m;
	    String tmp = ""; 
	    String priceIdToString = priceIdList.toString();
	    int maxCount = 0;
        String maxString = "";
        for (String priceId : priceIdList) {
			if(tmp.equals(priceId)){
				continue;
			}
			tmp = priceId;  
            regex = priceId;
            p = Pattern.compile(regex);  
            m = p.matcher(priceIdToString); 
            int count = 0;
            while(m.find()) {  
            	count++;  
            }
            if(count > maxCount){
            	maxCount = count;
            	maxString = priceId;
            }
		}
        if(maxCount != specValueList.size()){
        	return Result.failure("所选规格不存在");
        }
        ProductPrice productPrice = productPriceService.findOne(maxString);
		float quantity = warehouseProductService.findQuantityByProductPriceId(maxString);
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("productPrice", productPrice);
		map.put("quantity", quantity);

		return Result.success(map);
	}
	
	/**
	 * 展示列表页面
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:view")
	@RequestMapping(value = "/{page}")
	public ModelAndView view(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page) throws Exception {
		return new ModelAndView("bussiness/orderlang/productPrice/"+page, modelMap);
	}
	
	/**
	 * 根据ID查询单条数据
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:select")
	@RequestMapping(value = "/data/{id}")
	@ResponseBody
	public Object data(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String id) throws Exception {
		ProductPrice productPrice = productPriceService.findOne(id);
		return productPrice;
	}
	
	/**
	 * 根据ID查询单条数据并返回相应表单
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:view")
	@RequestMapping(value = "/form/{page}/{id}")
	public ModelAndView form(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page, @PathVariable String id) throws Exception {
		ProductPrice productPrice = productPriceService.findOne(id);
		modelMap.put("productPrice", productPrice);
		return new ModelAndView("bussiness/orderlang/productPrice/"+page, modelMap);
	}
	
	/**
	 * 获取列表展示数据
	 * @param modelMap
	 * @param productPrice
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:select")
	@RequestMapping(value = "/list/data", method=RequestMethod.POST)
	@ResponseBody
	public Object listData(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductPrice productPrice) throws Exception {
		List<ProductPrice> productPriceList = productPriceService.findList(productPrice);
		int total = productPriceService.findTotal(productPrice);
		EasyUIDataGrid eudg = new EasyUIDataGrid();
		eudg.setTotal(String.valueOf(total));
		eudg.setRows(productPriceList);
		return eudg;
	}
	
	/**
	 * 获取列表展示数据带分页器用于jsp自己做分页
	 * @param modelMap
	 * @param productPrice
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:view")
	@RequestMapping(value = "/list/page", method=RequestMethod.POST)
	@ResponseBody
	public Object listPage(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductPrice productPrice) throws Exception {
		List<ProductPrice> productPriceList = productPriceService.findList(productPrice);
		int total = productPriceService.findTotal(productPrice);
		Page<ProductPrice> page = new Page<ProductPrice>(productPrice.getPage(), productPrice.getRows(), total);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("rows", productPriceList);
		dataMap.put("page", page);
		return dataMap;
	}
	
	/**
	 * 获取所有数据
	 * @param modelMap
	 * @param productPrice
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:select")
	@RequestMapping(value = "/list/all", method=RequestMethod.POST)
	@ResponseBody
	public Object listAll(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductPrice productPrice) throws Exception {
		List<ProductPrice> productPriceList = productPriceService.findAll(productPrice);
		return productPriceList;
	}
	
	/**
	 * 保存新增的数据
	 * @param modelMap
	 * @param productPrice
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:insert")
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	@ResponseBody
	public String save(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductPrice productPrice) throws Exception {
		if (!beanValidator(modelMap, productPrice)){
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		productPriceService.save(productPrice);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 保存修改的数据
	 * @param modelMap
	 * @param productPrice
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:update")
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductPrice productPrice) throws Exception {
		if (!beanValidator(modelMap, productPrice)){
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		productPriceService.update(productPrice);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 删除数据库
	 * @param modelMap
	 * @param productPriceId
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:delete")
	@RequestMapping(value = "/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, String id) throws Exception {
		List<String> listId = Arrays.asList(id.split(","));
		productPriceService.delete(listId);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 假删除
	 * @param modelMap
	 * @param productPrice
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:delete")
	@RequestMapping(value = "/remove", method=RequestMethod.POST)
	@ResponseBody
	public String remove(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductPrice productPrice) throws Exception {
		productPriceService.remove(productPrice);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}

	/**
	 * 导出Excel数据
	 * @param modelMap
	 * @param productPrice
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productPrice:productPrice:export")
	@RequestMapping(value = "/export")
	public String export(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductPrice productPrice) throws Exception {
		String fileName = DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
		List<ProductPrice> productPriceList = productPriceService.findAll(productPrice);
		new ExportExcel(null, ProductPrice.class).setDataList(productPriceList).write(response, fileName).dispose();
		return null;
	}
}